package com.aaa.product.controller;

import com.aaa.product.entity.ShoppingCart;
import com.aaa.product.entity.TbActInf;
import com.aaa.product.service.impl.ShoppingCartServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class SCartController {

    @Resource
    ShoppingCartServiceImpl shoppingCartServiceImpl;

    @RequestMapping("selectByPhone")
    public List<ShoppingCart> selectByPhone(String phone){
        return shoppingCartServiceImpl.selectByPhone(phone);
    }

    @RequestMapping("delete")
    public boolean delete(String sid){
        return shoppingCartServiceImpl.removeById(sid);
    }

    @RequestMapping("gotoCart")
    public boolean gotoCart(ShoppingCart shoppingCart){
        Integer nodup = shoppingCartServiceImpl.nodup(shoppingCart.getPhone(), shoppingCart.getTid());
        if (nodup>0){
            return shoppingCartServiceImpl.countAdd(shoppingCart.getPhone(), shoppingCart.getTid());
        }else {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(date);
            shoppingCart.setUpdateDate(dateString);
            return shoppingCartServiceImpl.saveOrUpdate(shoppingCart);
        }
    }
}
